/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supportlearning.session;

import java.util.Date;
import javax.ejb.Stateless;
import javax.management.relation.Role;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import supportlearning.entities.Account;
import supportlearning.entities.CMSRole;

/**
 *
 * @author Huy
 */
@Stateless
public class createuser implements createuserLocal {

    @PersistenceContext(unitName = "eprojectSupportLearning-ejbPU")
    private EntityManager em;

    public int CreateUser(String User, String pass, String FirstN, String LastN, String Addr, boolean Gend, Date CDate, String Email, boolean Status, String avatar, int roleid) {

        CMSRole cmsrole = em.find(CMSRole.class, roleid);
        if (cmsrole != null) {
            Account acc = new Account();
            acc.setAccountId(User);
            acc.setPassword(pass);
            acc.setFirstName(FirstN);
            acc.setLastName(LastN);
            acc.setAddress(Addr);
            acc.setGender(Gend);
            acc.setCreateDate(CDate);
            acc.setEmail(Email);
            acc.setStatus(Status);
            acc.setAvatar(avatar);
            acc.setCMSRole(cmsrole);
            em.persist(acc);
            return 2;//OK
        } else {
            return 3;//RoleID is valid
        }



    }

    public void persist(Object object) {
        em.persist(object);
    }
}
